package org.yzh.web.controller;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.yzh.web.component.mybatis.Page;
import org.yzh.web.component.mybatis.PageInfo;
import org.yzh.web.component.mybatis.Pagination;
import org.yzh.web.model.APIResult;
import org.yzh.web.model.vo.*;
import org.yzh.web.service.AlarmCountHourService;
import org.yzh.web.service.AlarmCountService;
import org.yzh.web.service.AlarmMessageService;

import java.util.List;

/**
 * @author lsj
 * @date 2021/1/14 11:57
 */
@RestController
public class AlarmMessageController {

    @Autowired
    private AlarmMessageService alarmMessageService;

    @Autowired
    private AlarmCountService alarmCountService;

    @Autowired
    private AlarmCountHourService alarmCountHourService;

    @Operation(summary = "报警消息列表")
    @GetMapping("alarms")
    public APIResult<Pagination<AlarmMessage>> find(String deviceId, String plateNo, Integer type, LocalDate startDate,
        LocalDate endDate, LocalDateTime startDateTime, LocalDateTime endDateTime,
        @RequestParam(value = "types[]", required = false) List<Integer> types, PageInfo pageInfo) {
        AlarmMessageQuery query = new AlarmMessageQuery();
        query.setDeviceId(deviceId);
        query.setPlateNo(plateNo);
        query.setType(type);
        query.setTypes(types);
        query.setStartDateTime(startDateTime);
        query.setEndDateTime(endDateTime);
        query.setStartDate(startDate);
        query.setEndDate(endDate);
        return APIResult.success((Pagination<AlarmMessage>)Page.start(() -> alarmMessageService.find(query), pageInfo));
    }

    @Operation(summary = "报警消息总数")
    @GetMapping("alarms/counts/total")
    public APIResult<AlarmCountTotal> findAlarmCountTotal(AlarmMessageQuery query) {
        return APIResult.success(alarmMessageService.findAlarmCountTotal(query));
    }

    @Operation(summary = "设备报警消息统计")
    @GetMapping("alarms/counts")
    public APIResult<CountAndSummary<AlarmCount>> findAlarmCount(AlarmCountQuery query, PageInfo pageInfo) {
        return APIResult.success(alarmCountService.findAlarmCount(query, pageInfo));
    }

    @Operation(summary = "设备报警消息分时段统计")
    @GetMapping("alarms/counts/hours")
    public APIResult<List<AlarmCountHour>> findAlarmCountHour(AlarmCountHourQuery query) {
        return APIResult.success(alarmCountHourService.findAlarmCountHour(query));
    }

    @Operation(summary = "车辆运行分析")
    @GetMapping("mileage_alarms")
    public APIResult<Pagination<MileageAlarm>> findMileageAlarms(DateTimeQuery query, PageInfo pageInfo) {
        return APIResult
            .success((Pagination<MileageAlarm>)Page.start(() -> alarmCountService.findMileageAlarm(query), pageInfo));
    }
}
